package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }

}
class Solution {
    // a b b c b b d
    // 0 1 2 3 4 5 6

    // a b b c c b b d
    // 0 1 2 3 4 5 6 7
    public String longestPalindrome(String s) {
        char[] str = s.toCharArray();
        int n = s.length();
        int begin = 0, end = 0;
        int len = 0;
        for (int i = 0; i < n; i++) {
            //固定s[i]这个字符
            //1.从当前元素向左右进行扩展,分奇偶数两种情况

            //1.奇数情况
            int left = i - 1, right = i + 1;

            while(left >= 0 && right < n){
                if(str[left] == str[right]){
                    left--;
                    right++;
                }else break;
            }
            //统计子回文串的长度
            int count =  right - left - 1;
            if(count > len) {
                begin = left + 1;
                end = right - 1;
                len = count;
            }

            //2.偶数情况
            left = i;
            right = i + 1;

            while(left >= 0 && right < n){
                if(str[left] == str[right]){
                    left--;
                    right++;
                }else break;
            }
            //统计子回文串的长度
            count =  right - left - 1;
            if(count > len) {
                begin = left + 1;
                end = right - 1;
                len = count;
            }
        }

        return new String(str, begin, end - begin + 1);
    }
}